<template>
  <div class="app-container">
    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-input
        v-model="listQuery.name"
        clearable
        class="filter-item"
        style="width: 200px;"
        placeholder="请输入管理员名称"
      />
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        查找
      </el-button>
      <el-button
        class="filter-item"
        v-permission="'Admin::create'"
        type="primary"
        icon="el-icon-edit"
        @click="handleCreate"
      >
        添加
      </el-button>
    </div>

    <!-- 查询结果 -->
    <el-table
      v-loading="listLoading"
      :data="tableData.data"
      size="small"
      element-loading-text="正在查询中。。。"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="管理员ID" prop="admin_id" sortable />
      <el-table-column align="center" label="管理员名称" prop="admin_name" />
      <!-- 操作按钮组件 -->
      <action-button
        :handle-update="handleUpdate"
        :handle-delete="handleDelete"
        :loading="deleteLoading"
        update-auth="Admin::update"
        delete-auth="Admin::delete"
        :disable-update="(item) => item.admin_id === 1"
        :disable-delete="(item) => item.admin_id === 1"
      />
    </el-table>
    <!-- 分页组件 -->
    <pagination
      v-show="tableData.total > 0"
      :total="tableData.total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    />

    <!-- 添加或修改对话框 -->
    <dialog-form
      ref="dataForm"
      :status="dialogStatus"
      :rules="rules"
      :data="dataForm"
      :loading="dialogFormLoading"
      @createData="createData"
      @updateData="updateData"
      width="500px"
    >
      <template>
        <el-form-item label="管理员名称" prop="admin_name">
          <el-input v-model="dataForm.admin_name" />
        </el-form-item>
        <el-form-item label="登录密码" prop="password">
          <el-input v-model="dataForm.password" />
        </el-form-item>
        <el-form-item label="绑定角色" prop="role_id">
          <el-select v-model="dataForm.role_id" placeholder="请选择">
            <el-option
              v-for="item in roles"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </template>
    </dialog-form>
  </div>
</template>

<script>
import { listAdmin, createAdmin, updateAdmin, deleteAdmin } from '@/api/admin'
import { listRole } from '@/api/role'
import table from '@/mixins/table'

export default {
  name: 'Admin',
  mixins: [table],
  data() {
    return {
      pageTitle: '管理员',
      rules: {
        admin_name: [{ required: true, message: '管理员名称不能为空', trigger: 'blur' }],
        password: [{ required: false, message: '管理员密码不能为空', trigger: 'blur' }],
        role_id: [{ required: true, message: '绑定角色不能为空', trigger: 'blur' }]
      },
      roles: [],
      getListApi: listAdmin,
      createApi: createAdmin,
      updateApi: updateAdmin,
      deleteApi: deleteAdmin
    }
  },
  created() {
    this.getRoles()
  },
  methods: {
    resetForm() {
      this.dataForm = {
        admin_id: undefined,
        admin_name: undefined,
        password: undefined,
        role_id: undefined
      }
    },
    async getRoles() {
      const { err, res } = await listRole()
      if (err === null) {
        this.roles = res.map((item) => {
          return {
            label: item.role_name,
            value: item.role_id
          }
        })
      }
    }
  }
}
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #20a0ff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 120px;
  height: 120px;
  line-height: 120px;
  text-align: center;
}
.avatar {
  width: 120px;
  height: 120px;
  display: block;
}
.el-tag + .el-tag {
  margin-left: 10px;
}
.el-tag + .el-tag {
  margin-left: 10px;
}
.button-new-tag {
  margin-left: 10px;
  height: 32px;
  line-height: 30px;
  padding-top: 0;
  padding-bottom: 0;
}
.input-new-tag {
  width: 90px;
  margin-left: 10px;
  vertical-align: bottom;
}
</style>
